import matplotlib.pyplot as plt
import random

class My_Pie:
    def __init__(self,labels,proportion,
                 float_z=2,
                 title="Add the title",
                 legend="Add the title",
                 colors=None,):
        self.float_z = float_z
        self.proportion = proportion
        self.labels = labels
        self.title = title
        self.legend = legend
        match colors:
            case None:
                self.colors = ["#{0:2X}{1:2X}{2:2X}".format(
                    random.randint(17, 255),
                    random.randint(17, 255),
                    random.randint(17, 255))
                    for i in range(len(self.proportion))]
            # case "blue":
            #     self.colors = ["#{0:2X}{1:2X}{2:2X}".format(
            #         random.randint(17, 130),
            #         random.randint(17, 130),
            #         random.randint(180, 255))
            #         for i in range(len(self.proportion))]
            # case "green":
            #     self.colors = ["#{0:2X}{1:2X}{2:2X}".format(
            #         random.randint(17, 130),
            #         random.randint(180, 255),
            #         random.randint(17, 130))
            #         for i in range(len(self.proportion))]
            # case "red":
            #     self.colors = ["#{0:2X}{1:2X}{2:2X}".format(
            #         random.randint(180, 255),
            #         random.randint(17, 130),
            #         random.randint(17, 130))
            #         for i in range(len(self.proportion))]
    def show_pie(self):
        explode = [0]*len(self.proportion) # 突出显示第一部分（可选）
        explode[self.proportion.index(max(self.proportion))] = 0.1

        plt.figure(figsize=(8, 6))  # 设置画布大小
        self.pie = plt.pie(
            self.proportion,
            labels=self.labels,
            colors=self.colors,
            explode=tuple(explode),
            autopct=f'%1.{self.float_z}f%%',  # 显示百分比格式
            shadow=True,  # 添加阴影
            startangle=90  # 起始角度（0度从x轴开始）
          )

        plt.title(self.title)
        plt.axis('equal')  # 保证饼图是正圆形
        plt.legend(self.labels, loc="best", title=self.legend)

        # 收尾工作: 保存与返回路径
        name = rf"D:\python\pie{random.randint(1, 2000)}.jpg"
        plt.savefig(name)
        # plt.show()
        return name

if __name__ == '__main__':
    pie = My_Pie(["a","b","c","d","e"],[1,2,3,4,5])
    pie.show_pie()

































